import {
	createStore
} from "vuex";
import apiLogin from '@/util/api/login.js'
import apiUser from '@/util/api/user.js'
import apiCode from '@/util/api/code.js'
import apiAddress from '@/util/api/address.js'
const store = createStore({
	// 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。
	state: {
		// 个人中心用户信息
		userInfo: {
			avator: '',
			name: '',
			account: '',
			nickname: '',
			phone: ''
		},
		wayType: {}, //报单工单模块方式 
		appointmentTime: null, //报单预约时间
		checkBrandItem: {
			id: null
		}, //选中品牌
		checkClassifyItem: {
			id: null
		}, //选中分类
		addressList: [], //地址列表
		total: 0, //地址列表总条数
		is_new_addressItemId: '', //新增客户地址信息id
		getdetailData: null, //编码详细数据
		orderGoodsTypes: [], //报单商品
		checkedFaultList: [], //故障库原因,
		repairGoodsVos: [], //返厂商品
		selectVoGoods: [], //返厂无条码商品
		chackFaultItem: {
			id: null
		}, //当前选中的故障类型
		userOrder: {
			"address": "", //客户详细地址
			"area": "", //地址-县/区
			"city": "", //地址-市
			"helpOtherCreateOrder": false, //	是否代他人建单：0-否，1-是	
			"name": "", //客户姓名
			"otherPhone": "", //其他电话
			"phone": "", //客户手机号
			"province": "", //地址-省
			"town": "", //地址-镇
			"userId": null
		}, //报单时选中显示的地址
		userMobileHasLogin: false //判断是否要登录 true需要 false不需要
	},
	mutations: {
		// 通过提交commit改变数据
		// 更新state数据，用于修改状态(同步)
		sel_val(state, val) {
			state.sel_val = val
		},
		setStateAttr(state, info) {
			state.userInfo = {
				...info
			}
		},
		setStatedetailData(state, res) {
			state.getdetailData = res
		},
		setAddressList(state, list, page = 0) {
			if (page > 1) {
				state.addressList = [...state.addressList, ...list]
			} else {
				state.addressList = [...list]
			}
		},
		setTotal(state, n) {
			state.total = n
		},
		setWayType(state, val) {
			state.wayType = val
		},
		setUserOrder(state, val) {
			state.userOrder = val
		},
		setorderGoodsTypes(state, val) {
			state.orderGoodsTypes = val
		},
		setorderGoodsTypesItem(state, val) {
			state.orderGoodsTypes.push(...val)
		},
		setrepairGoodsVos(state, val) {
			state.repairGoodsVos = val
		},
		set_SelectVoGoods(state, val) {
			state.selectVoGoods = val
		},
		setcheckBrandItem(state, val) {
			state.checkBrandItem = val
		},
		setcheckClassifyItem(state, val) {
			state.checkClassifyItem = val
		},
		setchackFaultItem(state, val) {
			state.chackFaultItem = val
		},
		setchackFaultList(state, val) {
			state.checkedFaultList = val
		},
		setappointmentTime(state, val) {
			state.appointmentTime = val
		},
		set_userMobileHasLogin(state, val) {
			state.userMobileHasLogin = val
		},
		set_is_new_addressItemId(state, val) {
			state.is_new_addressItemId = val
		},
		// 删除商品
		deleteGoods(state, item) {
			if (!item.specId) {
				item.specId = item.goodsSpecId
			}
			let orderGoodsTypes = state.orderGoodsTypes.filter(o => {
				if (o.type == 1 && item.typeGoods == 1) {
					o.orderGoodsVos = o.orderGoodsVos.filter(k => {
						if (k.codeIds[0] != item.codeIds[0]) {
							return k
						}
					})
					// 如果客户已登记商品为空，则删除该客户已登记商品不返回
					if (o.orderGoodsVos.length == 0) {
						return !o
					}
				} else if (o.type == 3 && item.typeGoods == 3) {
					o.orderGoodsVos = o.orderGoodsVos.filter(k => {
						if (k.goodsSpecId != item.specId) {
							return k
						}
					})
					// 如果分类为空，则删除该分类不返回
					if (o.orderGoodsVos.length == 0) {
						return !o
					}
				}
				return o
			})
			state.orderGoodsTypes = orderGoodsTypes
		}
	},
	actions: {
		// 定义actions，用于修改状态(异步)
		// 2秒后更新状态
		updateUid(context, payload) {
			setTimeout(() => {
				context.commit('updateUid', payload)
			}, 2000)
		},
		// 刷新token
		async getrefreshToken() {
			const refreshToken = uni.getStorageSync('access-user').refreshToken //刷新token
			return apiLogin.refreshToken({
				refreshToken: refreshToken
			})
		}, //获取用户信息
		async getUpdataUser(context) {
			await apiUser.getUerDetail().then(res => {
				if (res.code == 20000) {
					context.commit('setStateAttr', res.data.row)
				}
			})
		},
		// 获取编码详情
		async getDetail(context, code) {
			uni.showLoading({
				mask: true,
				title: '加载中...'
			})
			await apiCode.getUserOrderCodeInfo({
				code: code
			}).then(res => {
				context.commit('setStatedetailData', res)
			})
			uni.hideLoading()
		},
		// 获取地址列表
		async seleAddress(context, {
			page,
			name = ""
		}) {
			let data = {
				"limit": 0,
				name: name,
				"page": page
			}
			uni.showLoading({
				mask: true,
				title: '加载中...'
			})
			await apiAddress.getUserRegisterAddressList(data).then(res => {
				if (res.code == 20000) {
					context.commit('setAddressList', res.data.rows, page)
					context.commit('setTotal', res.data.total)
				}
			})
			uni.hideLoading()
		},
		async getMobileUserHasLogin(context, page) {
			const res = await apiLogin.getMobileUserHasLogin()
			if (res.code == 20000) {
				context.commit('set_userMobileHasLogin', res.data.userMobileHasLogin)
			}
		}
	},
	getters: {
		// getter（获取数据)
		// 处理隐藏手机号
		setProcessingTel(state) {
			let str = state.userInfo.phone
			let tem = str.slice(0, 3) + '****' + str.slice(str.length - 4)
			return tem
		},
		// 商品明细总数
		getTotalNum(state) {
			let str = []
			for (let item of state.orderGoodsTypes) {
				str.push(item.orderGoodsVos.length)
			}
			if (str.length == 0) {
				return 0
			} else {
				return str.reduce((t, o) => t + o)
			}
		},
		// 获取明细商品列表
		getTotalList(state) {
			let list = []
			for (let item of state.orderGoodsTypes) {
				list.push(...item.orderGoodsVos)
			}
			return list
		}
	},
	modules: {}
});
export default store